package game.screen;
import java.nio.FloatBuffer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;import game.entity.AbsSprite;
import game.entity.Army;
import game.entity.BufferSpriteManager;
import game.entity.Monster;
import game.entity.SpriteManager;
import game.entity.Weapon;
import game.helper.Input;
import game.helper.Lightning;
import game.helper.MovingLeft;
import game.helper.MovingRight;
import game.helper.Shot;
import game.logic.GameControl;
import game.shared.Constants;
import game.shared.Resources;
import game.windows.Button;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;

public class ScreenPlaying  extends AbsScreen{

	SpriteManager armyManager;
	SpriteManager weaponManager;
	SpriteManager monterManager;
	BufferSpriteManager bufferSpriteManager;
	SpriteBatch sb;
	Constants constants;
	float timer;
	private BitmapFont font;
	Texture background;
	Monster monter;
	
	ShapeRenderer shapeRenderer;
	Camera camera;
	Rectangle viewport;
	
	Button button;
	
	public ScreenPlaying(GameControl _gameControl) {
		super(_gameControl);
		armyManager = new SpriteManager();
		weaponManager = new SpriteManager();
		monterManager = new SpriteManager();
		bufferSpriteManager = new BufferSpriteManager(armyManager, monterManager, weaponManager);
		sb = new SpriteBatch();
		//LoadResource();
		timer = 0;
		font = new BitmapFont();
		constants = new Constants();
		shapeRenderer = new ShapeRenderer();
		camera = new OrthographicCamera(Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
		camera.update();
		shapeRenderer.setProjectionMatrix(camera.combined);
		viewport = new Rectangle(0, 0, 100, 100);
		Resources.LoadResource("res/resources.txt");
		button = new Button(Resources.getTextureRegion(1), Resources.getTextureRegion(2));
		button.setXY(155,270);
		
		LoadResource(); //Phai luon de duoi cung.
	}
	

	@Override
	public void update() {
		button.update();
		
		CheckInsection();
		CheckShotMonster();
		armyManager.update();
		weaponManager.update();
		monterManager.update();
		
		timer += Gdx.graphics.getDeltaTime();
		if (timer > 1.5){
			timer = 0;
			AddResource();
		}
	}

	private void CheckInsection() {
		for(int i=0; i<armyManager.count(); i++)
		{
			AbsSprite army = armyManager.getByIndex(i);
			com.badlogic.gdx.math.Rectangle armyRect = army.getBoundingRectangle();
			boolean bOver = false;
			for(int j=0; j<monterManager.count(); j++)
			{
				AbsSprite monter = monterManager.getByIndex(j);
				com.badlogic.gdx.math.Rectangle monterRect = monter.getBoundingRectangle();
				
				if(armyRect.overlaps(monterRect))
				{
					army.setState(Constants.SPRITE_ATTACK_RIGHT);
					monter.setMoving(new MovingRight(monter.getSpeed()));
					bOver = true;
				}
				if(!bOver){
					army.setState(Constants.SPRITE_STAND_RIGHT);
				}
			}				
		}
	}

	private void CheckShotMonster() {
		for(int i=0; i<weaponManager.count(); i++){
			AbsSprite weapon = weaponManager.getByIndex(i);
			Rectangle weaponRect = weapon.getBoundingRectangle();
			for(int j=0; j<monterManager.count();j++){
				AbsSprite monster = monterManager.getByIndex(j);
				Rectangle monsterRect = monster.getBoundingRectangle();
				if(weaponRect.overlaps(monsterRect)){
					weaponManager.remove(i);
					monterManager.remove(j);
				}
			}
		}		
	}

	@Override
	public void render(float arg0) {
		sb.begin();
		sb.draw(background, 0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT, 0, 0, 480, 320, false, false);		
		armyManager.render(sb);
		weaponManager.render(sb);
		monterManager.render(sb);
	
		font.draw(sb, String.valueOf(timer),Constants.SCREEN_WIDTH - 100, Constants.SCREEN_HEIGHT - 100);

		button.render(sb);
		sb.end();
		
		armyManager.renderShaper();
	}
	
	private void LoadResource() {
		Monster monter_2 = new Monster((Monster)bufferSpriteManager.getSpriteBufferByID(Constants.BUFFER_SPRITE_MONSTER));
		monter_2.setMoving(new MovingLeft(2f));
		monter_2.setPosition(400, 130);
		monterManager.add(monter_2);
		
		Army army_castle = new Army(Constants.BUFFER_SPRITE_ARMY, monterManager, "res/img_castle/level_1.txt");
		army_castle.setPosition(0, 20);
		armyManager.add(army_castle);
		
		Army army =  new Army((Army)bufferSpriteManager.getSpriteBufferByID(Constants.BUFFER_SPRITE_ARMY));
		army.setPosition(25, 150);
		army.setInput(new Input(army));
		Lightning lightning = new Lightning(camera, viewport, shapeRenderer);
		army.setAbility(lightning);
		armyManager.add(army);
		
		Weapon gun = new Weapon((Weapon)bufferSpriteManager.getSpriteBufferByID(Constants.BUFFER_SPRITE_WEAPON));
		Shot shot = new Shot(gun, Constants.ROCKET_WHITE, 2f, weaponManager, 1.5f);
		gun.setAbility(shot);
		weaponManager.add(gun);
		
		background = new Texture("res/img_background/bg_level_1.png");
	}
	
	public void AddResource(){
		Monster monster_3 = new Monster((Monster)bufferSpriteManager.getSpriteBufferByID(Constants.BUFFER_SPRITE_MONSTER));
		monster_3.setMoving(new MovingLeft(1.5f));
		monster_3.setPosition(400, 30);
		monterManager.add(monster_3);
	}
	/*
	public void aaa()
	{
		// turns on lighting
		 GLCommon gl;
		gl.glEnable(GL10.GL_LIGHTING);
		 

		// Enable up to n=8 light sources: GL_LIGHTn
		 gl.glEnable(GL10.GL_LIGHT0);
		 gl.glEnable(GL10.GL_LIGHT1);
		 gl.glEnable(GL10.GL_LIGHT2);
		 gl.glEnable(GL10.GL_LIGHT3);
		 gl.glEnable(GL10.GL_LIGHT4);
		 

		// light position
		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, new float[]{0, 0, 1, 1}, 0);
		 

		// Light that has been reflected by other objects and hits the mesh in small amounts
		 gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, new float[]{0.005f, 0.005f, 0.005f, 1f}, 0);
		 

		// setting diffuse light color like a bulb or neon tube
		 gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, new float[]{0.9f, 0.9f, 0.7f, 1f}, 0);
		 

		// setting specular light color        like a halogen spot
		 gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_SPECULAR, new float[]{0.9f, 0.9f, 0.7f, 1f}, 0);
		 

		// directional or positional light
		 // vector4f is x,y,z + w component
		 // w=0 to create a directional light (x,y,z is the light direction) like the sun
		 // w=1 to create a positional light like a fireball.
		 !FloatBuffer fb = !BufferUtils.newFloatBuffer(8);
		 fb.put(new float[]{1, 1, 1, 1});
		 gl.glLightfv(GL10.GL_LIGHT3, GL10.GL_POSITION, fb);
		 

		// spotlight
		 // high exponent values makes light cone's center brighter
		 !FloatBuffer fb_pos = !BufferUtils.newFloatBuffer(8);
		 fb_pos.put(new float[]{1, 1, 1, 1});
		 !FloatBuffer fb_dir = !BufferUtils.newFloatBuffer(8);
		 fb_dir.put(new float[]{2, 2, 2});
		 gl.glLightfv(GL10.GL_LIGHT4, GL10.GL_POSITION, fb_pos);
		 gl.glLightfv(GL10.GL_LIGHT4, GL10.GL_SPOT_DIRECTION, fb_dir);
		 gl.glLightf(GL10.GL_LIGHT4, GL10.GL_SPOT_CUTOFF, angle); // angle [0..180]

		gl.glLightf(GL10.GL_LIGHT4, GL10.GL_SPOT_EXPONENT, exp); // exponent [0..128]

		// defines how light amount reduces if model gets away from light source
		 // GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION
		 gl.glLightf(GL10.GL_LIGHT0, GL10.GL_CONSTANT_ATTENUATION, .05f);
	}*/
}
